/**
 *
 */
package com.moogene.saad.gameplay.bullet;

import java.util.ArrayList;

import com.moogene.android.engine.animlite.Animlite;
import com.moogene.android.engine.mmi.Layer;
import com.moogene.saad.gameplay.attacter.Attacker;

/**
 * @author 28849218
 * 
 */
public class Bullet extends Layer {
	private Attacker target;

	private Animlite anim;

	final private Effectable effectable;
	
	private Physics physics;

	public Bullet(Effectable effectable, Animlite anim) {
		super(0, 0);
//		physics = Physics.getPhysics(anim, this);
//		if (anim != null) {
//			this.width = anim.getFrameWidth();
//			this.height = anim.getFrameHeight();
//		}
//		else{
//			this.width = anim.getFrameWidth();
//			this.height = anim.getFrameHeight();
//			physics = Physics.getPhysics(anim, this);
//			collision = Collision.getCollision(anim.getResId());
//		}
		setPriority(Layer.TOP);
		this.effectable = effectable;
		this.anim = anim;
	}
	
	@Override
	protected boolean canAutoRemove(){
		return true;
	}

	@Override
	public void update() {
		super.update();
		ArrayList<Attacker> attBingos = physics.updateAndGetBingos();
		for(Attacker a: attBingos){
			effectable.bingo(a);
		}
//		boolean isBingo = physics.updateAndCheckBingo();
//		if (isBingo) {
//			effectable.bingo(target);
//		}
//		boolean isBingo = false;
//		if (speed == -1) {
//			isBingo = true;
//		}
//		if(collision != null){
//			isBingo = collision.isBingo(target);
//		}
//		if (isBingo) {
//			effectable.bingo(target);
////			Book.getInstance().getCurrentPage().removeLayer(this);
//		}
//		x += vX;
//		y += vY;
	}

	@Override
	protected Animlite getAttachedAnimlite() {
		return anim;
	}
	
	public void shoot(Attacker attacker){
		this.target = attacker;
		physics = Physics.getPhysics(anim, this);
		physics.shoot(attacker);

//		if(speed != -1){
//			anim.getRotateAnim().setDegree(attacker.getX(), attacker.getY(), x, y);
//			int dis = MooMath.slowDistance(attacker.getX() - x, attacker.getY() - y);
//			vX = speed * (attacker.getX() - x) / dis;
//			vY = speed * (attacker.getY() - y) / dis;
//			collision.setup(this);
//		}
	}

//	public int getVX() {
//		return vX;
//	}
//
//	public int getVY() {
//		return vY;
//	}

	@Override
	public Bullet clone() {
		Bullet b = (Bullet) super.clone();
		if(anim != null){
			b.anim = anim.copy();
		}
		return b;
	}
}
